package com.rightsidetech.activity.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.activity.dto.request.store.MtActivityActStoreIdReq;
import com.rightsidetech.activity.dto.response.store.MtActivityLuckInfoResp;
import com.rightsidetech.activity.dto.response.store.app.*;
import com.rightsidetech.activity.entity.*;
import com.rightsidetech.activity.mapper.*;
import com.rightsidetech.activity.service.MtActivityAppService;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.enums.StoreActivityEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("mtActivityAppService")
public class MtActivityAppServiceImpl implements MtActivityAppService {

    @Resource
    private MtActivityMapper mtActivityMapper;
    @Resource
    private MtActivityRuleMapper mtActivityRuleMapper;
    @Resource
    private MtUserJoinActivityApplyMapper mtUserJoinActivityApplyMapper;
    @Resource
    private MtUserJoinActivityProcessMapper mtUserJoinActivityProcessMapper;
    @Resource
    private MtUserJoinActivityPrizeMapper mtUserJoinActivityPrizeMapper;
    @Resource
    private MtActivityRuleDetailMapper mtActivityRuleDetailMapper;
    @Resource
    private MtActivityRuleBusinessMapper mtActivityRuleBusinessMapper;

    @Override
    public ResponseData<List<MtAppHomeResp>> list(String field){
        List<MtAppHomeResp> list = mtActivityMapper.selectAppList(field);
        return ResponseData.success(list);
    }

    @Override
    public MtActivity selectMtActivity(Long id) {
        return mtActivityMapper.selectByPrimaryKey(id);
    }

    @Override
    public MtActivityRule selectRuleByMoldAndActId(Long actId,String mold){
        return mtActivityRuleMapper.selectActRule(actId,mold);
    }

    @Override
    public MtActivityRule selectRuleByFieldAndActId(Long actId, String field) {
        return mtActivityRuleMapper.selectActRuleByField(actId,field);
    }

    @Override
    public int insertUserJoin(MtUserJoinActivityApply record){
        return mtUserJoinActivityApplyMapper.insertSelective(record);
    }

    @Override
    public MtUserJoinActivityApply selectUserJoinByIdAndUId(Long id,Long storeId){
        return mtUserJoinActivityApplyMapper.selectByActIdAndUserId(id,storeId);
    }

    @Override
    public MtAppDetailResp selectDetailById(Long id){
        return mtActivityMapper.selectDetailById(id);
    }

    @Override
    public MtActivity selectBaseById(Long id) {
        return mtActivityMapper.selectByPrimaryKey(id);
    }

    @Override
    public int processCount(Long actId,Long storeId){
        return mtUserJoinActivityProcessMapper.selectCountByActAndCode(actId,storeId);
    }

    @Override
    public MtUserJoinActivityProcess processByActIdAndCode(Long actId,Byte status,Long storeId){
        return mtUserJoinActivityProcessMapper.selectActAndCode(actId,status,storeId);
    }

    @Override
    public ResponseData insertProcess(MtUserJoinActivityProcess process) {
        return mtUserJoinActivityProcessMapper.insertSelective(process) > 0 ? ResponseData.success(CodeIdEnum.Success) : ResponseData.fail("上传失败");
    }

    @Override
    public ResponseData<PageInfo<MtActivityRecordResp>> recordPage(PageRequest req,Long storeId) {
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<MtActivityRecordResp> list = mtUserJoinActivityApplyMapper.selectRecordList(storeId,StoreActivityEnum.HJ000.getDicType());
        return ResponseData.success(new PageInfo<>(list));
    }

    @Override
    public List<MtActivityOrderResp> orderAndActivity(Long storeId){
        Map<String,Object> map = new HashMap<>();
        map.put("list", Arrays.asList(StoreActivityEnum.HJ001.getDicCode(),StoreActivityEnum.HJ004.getDicCode()));
        map.put("storeId",storeId);
        return mtUserJoinActivityApplyMapper.selectActOrderResp(map);
    }

    @Override
    public List<String> selectProcessPic(Long storeId,Long actId){
        return mtUserJoinActivityProcessMapper.selectPicByUserIdAndActId(storeId,actId);
    }

    @Override
    public List<CompareActResp> selectCompareAct(Long storeId,Long areaId){
        return mtUserJoinActivityProcessMapper.selectCompareAct(storeId,areaId);
    }

    @Override
    public int prizeCount(Long actId, Long storeId) {
        return mtUserJoinActivityPrizeMapper.selectCountByActIdAndStoreId(actId,storeId);
    }

    @Override
    public List<MtLotteryResp> selectLotteryResp(Long actId, Long storeId){
        return mtUserJoinActivityPrizeMapper.selectLotteryList(actId, storeId);
    }

    @Override
    public PageInfo<MtActivityLuckInfoResp> selectLuckResp(MtActivityActStoreIdReq req) {
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        List<MtActivityLuckInfoResp> resps = mtUserJoinActivityPrizeMapper.selectPrize(req);
        return new PageInfo<>(resps);
    }

    @Override
    public int updateProcess(MtUserJoinActivityProcess process) {
        return mtUserJoinActivityProcessMapper.updateByPrimaryKeySelective(process);
    }

    @Override
    public List<MtActivityRule> selectRuleList(Long actId, String mold) {
        return mtActivityRuleMapper.selectRuleList(actId,mold);
    }

    @Override
    public int updateRule(MtActivityRule rule) {
        return mtActivityRuleMapper.updateByPrimaryKeySelective(rule);
    }

    @Override
    public String selectRuleDetail(Long actId) {
        return mtActivityRuleDetailMapper.selectDetailByActId(actId);
    }

    @Override
    public List<String> selectPicListByActId(Long actId){
        return mtActivityRuleBusinessMapper.selectBizValueListByActId(actId);
    }

    @Override
    public List<MtActivity> selectHeadList(String dicCode) {
        return mtActivityMapper.selectHeadAct(dicCode);
    }

    @Override
    public List<MtUserJoinActivityProcess> selectProcessList(Long actId) {
        return mtUserJoinActivityProcessMapper.selectBaseListByActId(actId);
    }

    @Override
    public int updateUserJoin(Long actId, Long storeId,Short status) {
        return mtUserJoinActivityApplyMapper.updateStatusByActIdAndStoreId(actId,storeId,status);
    }

    @Override
    public MtActivityRuleBusiness selectBusinessByActIdAndComId(Long actId, String commodityId) {
        return mtActivityRuleBusinessMapper.selectByBizIdAndActId(actId,commodityId);
    }

    @Override
    public List<CompareActResp> selectCompareActJiZan(Long actId, Long areaId) {
        return mtUserJoinActivityProcessMapper.selectCompareActJiZan(actId,areaId);
    }

    @Override
    public int insertPrize(MtUserJoinActivityPrize prize) {
        return mtUserJoinActivityPrizeMapper.insertSelective(prize);
    }

    @Override
    public PageInfo<MtActivityRecordDetailResp> selectRecordDetail(PageRequest req,Long storeId){
        PageHelper.startPage(req.getPageNo(),req.getPageSize());
        return new PageInfo<>(mtActivityMapper.selectRecordDetailList(storeId));
    }
}
